﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLKS.ServiceReferenceQLKS;
namespace QLKS
{
    public partial class FrmPhong : Form
    {
        private IList<DMPHONG> lstPhong;
        public static FrmPhong frm = null;

        public FrmPhong()
        {
            InitializeComponent();
            InitPhong();
            InitLoaiPhong();
            frm = this;
        }

        private void InitPhong()
        {
            DMPHONG[] outdmphong;
            var client = new ServiceQLKSClient();
            var error = client.doparserPhong(out outdmphong, OPTCodeQLKS.OPTCode_PHONG_GETALL, null);
            if (error == ErrorCodeQLKS.CODE_PHONG_GETALL_SUCCESS)
            {
                lstPhong = outdmphong.ToList();
                dMPHONGBindingSource.DataSource = lstPhong;
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void InitLoaiPhong()
        {
            LOAIPHONG[] outloaiphong;
            var client = new ServiceQLKSClient();
            var error = client.doparserLoaiPhong(out outloaiphong, OPTCodeQLKS.OPTCode_LOAIPHONG_GETALL, null);
            if(error==ErrorCodeQLKS.CODE_LOAIPHONG_GETALL_SUCCESS)
            {
                lOAIPHONGBindingSource.DataSource = outloaiphong;
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void InsertOrUpdate()
        {
            DMPHONG dmphong = new DMPHONG();
            if (txtMaP.Text == null || txtMaP.Text == " ")
            {
                MessageBox.Show("Lỗi Mã Phòng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtMaP.Focus();
                return;
            }
            else
            {
                dmphong.MSPHONG = txtMaP.Text;
                dmphong.MSLOAIPHONG = cboLoaiP.SelectedValue.ToString();
                if (optTrong.Checked == true)
                {
                    dmphong.TINHTRANG = "0";
                }
                else
                {
                    dmphong.TINHTRANG = "1";
                }
            }
            DMPHONG[] outdmphong;
            var client = new ServiceQLKSClient();
            var error = client.doparserPhong(out outdmphong, OPTCodeQLKS.OPTCode_PHONG_INSERTUPDATE, dmphong);
            if (error == ErrorCodeQLKS.CODE_PHONG_INSERTUPDATE_SUCCESS)
            {
                InitPhong();
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void DeletePhong()
        {
            DMPHONG[] outdmphong;
            var client = new ServiceQLKSClient();
            var error = client.doparserPhong(out outdmphong, OPTCodeQLKS.OPTCode_PHONG_DELETE, new DMPHONG { MSPHONG = txtMaP.Text });
            InitPhong();
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            txtMaP.Clear();
            txtMaP.Focus();
            txtMaP.Enabled = true;
            optTrong.Checked = true;
        }

        private void gvPhong_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            var item = gvPhong.CurrentRow.DataBoundItem as DMPHONG;
            txtMaP.Text = item.MSPHONG;
            if (item.TINHTRANG.Equals("1"))
            {
                optKhongTrong.Checked = true;
            }
            else
            {
                optTrong.Checked = true;
            }
            foreach (LOAIPHONG lp in cboLoaiP.Items)
            {
                if (item.MSLOAIPHONG.Equals(lp.MSLOAIPHONG))
                {
                    cboLoaiP.SelectedItem = lp;
                    break;
                }
            }
        }

        private void cmdExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            InsertOrUpdate();
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {
            DeletePhong();
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            IList<DMPHONG> lsttmp;
            lsttmp = lstPhong.Where(x => x.MSPHONG.Contains(txttimkiem.Text)).ToList();
            dMPHONGBindingSource.DataSource = lsttmp;
        }

        private void FrmPhong_FormClosed(object sender, FormClosedEventArgs e)
        {
            frm = null;
        }
    }
}
